package cn.liu.jetpack_demo.pages.passport

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import cn.liu.jetpack_demo.api.PassportApi
import cn.liu.jetpack_demo.models.ResultModel
import cn.liu.jetpack_demo.routes.Screen
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import retrofit2.Response


@Composable
fun LoginPage(navController: NavController) {
    val coroutineScope = rememberCoroutineScope()

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            modifier = Modifier.clickable {
                navController.navigate(route = Screen.Detail.route)
            },
            text = "Login",
            color = Color.Magenta,
            fontWeight = FontWeight.Bold,
            fontSize = MaterialTheme.typography.headlineLarge.fontSize
        )
        Text(
            text = "Go Back",
            modifier = Modifier
                .padding(top = 150.dp)
                .clickable {
                    navController.popBackStack()
                })
        Button(onClick = {
            coroutineScope.launch {
                Log.d("Login -> ", "登录点击事件")

                PassportApi.syncLogin("hrbyuy","lyx123123") {
                    Log.d("Login -> ", "页面登录结果：$it")
                }
//                Log.d("Login -> ", "登录结果：$response")
            }

        }) {
            Text("异步登录")
        }
        Button(onClick = {
//            coroutineScope.launch {
            CoroutineScope(Dispatchers.IO).launch {
                Log.d("Login -> ", "同步登录点击事件")
                val response = PassportApi.login("liuchi188", "123456")
                Log.d("Login -> ", "同步登录结果：$response")
//            }
            }

        }) {
            Text("同步登录")
        }
    }
}

@Composable
@Preview
fun LoginPagePreview(showBackground: Boolean = true) {
    LoginPage(navController = rememberNavController())
}